package l2s.gameserver.scripts.custom;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import l2s.commons.db.DatabaseFactory;
import l2s.gameserver.model.L2World;
import l2s.gameserver.model.actor.instance.L2PcInstance;

public class VoteUtils
{
	public static VoteUtils getInstance()
	{
		return SingletonHolder._instance;
	}
	
	public static L2PcInstance loadPlayer(String charName)
	{
		L2PcInstance result = L2World.getInstance().getPlayer(charName);
		if (result == null)
		{
			try (Connection con = DatabaseFactory.getConnection())
			{
				PreparedStatement stm = con.prepareStatement("select charId from characters where char_name like ?");
				stm.setString(1, charName);
				ResultSet r = stm.executeQuery();
				if (r.next())
				{
					result = L2PcInstance.restore(r.getInt(1));
				}
				r.close();
				stm.close();
			}
			catch (SQLException e)
			{
				result = null;
			}
		}
		return result;
	}
	
	private static class SingletonHolder
	{
		protected static final VoteUtils _instance = new VoteUtils();
	}
	
	public static void onLoad()
	{
		VoteUtils.getInstance();
	}
}